const { getSqlLiteData } = require('../../SqlLiteDB');

module.exports = function setCustomerRouter (router) {

    //查询客户信息
    router.get('/getCustomers', async function (req, res, next) {
        try {
            let dataContent = JSON.parse(req.query['dataContent']);
            let name = dataContent.name;
            let SqlString = "SELECT * FROM jm_customer U"
                + (name ? ` WHERE U.name LIKE'${name}'` : '')
                + " ORDER BY U.name";
            let procedures = await getSqlLiteData(SqlString);

            res.status(200).json({
                procedures,
            });
        }
        catch (ex) {
            res.status(500).json({
                result: ex,
            });
            console.log('ex :>> ', {
                result: ex,
            });
        }
    });

    //修改或添加客户信息
    router.post('/updateCustomer', async function (req, res, next) {
        try {
            let dataContent = JSON.parse(req.body['dataContent']);
            let { wocusid, name, remark } = dataContent.customer;
            // let wocusid = dataContent.wocusid;
            // let name = dataContent.name;
            // let remark = dataContent.remark;
            // let user = dataContent.user;

            let SqlString = `REPLACE INTO jm_customer(wocusid,name,remark) 
            VALUES ('${wocusid}','${name}', '${remark}')`;
            let result = await getSqlLiteData(SqlString);

            res.status(200).json({
                result,
            });
        }
        catch (ex) {
            res.status(500).json({
                result: ex,
            });
            console.log('ex :>> ', {
                result: ex,
            });
        }
    });

    //删除采集记录
    router.post('/deleteCustomer', async function (req, res, next) {
        try {
            let dataContent = JSON.parse(req.body['dataContent']);
            let wocusid = dataContent.wocusid;

            let SqlString = `DELETE FROM jm_customer WHERE wocusid ='${wocusid}'`;
            let result = await getSqlLiteData(SqlString);

            res.status(200).json({
                result,
            });
        }
        catch (ex) {
            res.status(500).json({
                result: ex,
            });
            console.log('ex :>> ', {
                result: ex,
            });
        }
    });
};
